package caat.application.managedbean;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.icefaces.ace.component.submenu.Submenu;
import org.icefaces.ace.model.DefaultMenuModel;

import com.icesoft.faces.component.menubar.MenuItem;

import caat.application.dao.FunctionDAO;
import caat.application.entityobject.FunctionEO;
import caat.framework.AppAction;
import caat.framework.AppConstants;
import caat.framework.datamanager.Parameter;
import caat.framework.exception.DAOException;
import caat.framework.exception.DatabaseConnectionFailureException;
import caat.framework.exception.SelectStatementException;

public class FunctionMB extends AppAction {
	// bien khoi tao menu
	private DefaultMenuModel model;
	private Connection conn = null;
	// bien khoi tao menu bar
	@SuppressWarnings("rawtypes")
	private List lstMenuBar;

	@SuppressWarnings("rawtypes")
	public List getLstMenuBar() {
		return lstMenuBar;
	}

	public DefaultMenuModel getModel() {
		return model;
	}

	public void setModel(DefaultMenuModel model) {
		this.model = model;
	}

	public FunctionMB() {
		super();
//		getMenu();
		getMenuBar();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void getMenu() {
		String whereClause = null;
		Vector params = null;
		FunctionDAO funtionDAO = null;
		try {
			conn = getConnection();
			params = new Vector();
			funtionDAO = new FunctionDAO(conn);
			whereClause = "a.functionType=? ";
			params.add(new Parameter(AppConstants.TYPE_MENU_ADMIN, true));
			List<FunctionEO> lstFuntion = (List<FunctionEO>) funtionDAO
					.getFunctionAdmin(whereClause, params);
			if (lstFuntion != null) {
				model = new DefaultMenuModel();
				for (FunctionEO menu : lstFuntion) {
					/*
					 * root menu parentId = null
					 */
					if (menu.getParentId() == 0) {
						Submenu sub = setSubmenu(menu);
						model.addSubmenu(sub);
						org.icefaces.ace.component.menuitem.MenuItem mItemBase = null;
						for (FunctionEO fEO : lstFuntion) {
							if (fEO.getParentId() == menu.getFunctionId()) {
								mItemBase = setMenuItemACE(fEO);
								BuildSubmenuACE(mItemBase, fEO, lstFuntion);
								sub.getChildren().add(mItemBase);
							}
						}
					}

				}
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DatabaseConnectionFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SelectStatementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List getMenuBar() {
		String whereClause = null;
		Vector params = null;
		FunctionDAO funtionDAO = null;
		List<FunctionEO> lstFuntion = null;
		try {
			conn = getConnection();
			params = new Vector();
			lstMenuBar = new ArrayList();
			funtionDAO = new FunctionDAO(conn);
			whereClause = "a.functionType=? ";
			params.add(new Parameter(AppConstants.TYPE_MENU_ADMIN, true));
			lstFuntion = (List<FunctionEO>) funtionDAO.getFunctionAdmin(
					whereClause, params);
			if (lstFuntion != null) {
				MenuItem mItemBase = null;
				for (FunctionEO fEO : lstFuntion) {
					if (fEO.getParentId() == 0) {
						mItemBase = setMenuItem(fEO);
						BuildSubmenu(mItemBase, fEO, lstFuntion);
						lstMenuBar.add(mItemBase);
					}
				}

			}

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DatabaseConnectionFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SelectStatementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lstFuntion;
	}

	public void BuildSubmenu(MenuItem mBase, FunctionEO fEO,
			List<FunctionEO> lstFuntionChild) {
		try {
			for (FunctionEO eo : lstFuntionChild) {
				if (eo.getParentId() == fEO.getFunctionId()) {
					MenuItem mChild = setMenuItem(eo);
					mBase.getChildren().add(mChild);
					BuildSubmenu(mChild, eo, lstFuntionChild);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	public void BuildSubmenuACE(org.icefaces.ace.component.menuitem.MenuItem mBase, FunctionEO fEO,
			List<FunctionEO> lstFuntionChild) {
		try {
			for (FunctionEO eo : lstFuntionChild) {
				if (eo.getParentId() == fEO.getFunctionId()) {
					org.icefaces.ace.component.menuitem.MenuItem mChild = setMenuItemACE(eo);
					mBase.getChildren().add(mChild);
					BuildSubmenuACE(mChild, eo, lstFuntionChild);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	public Submenu setSubmenu(FunctionEO fEO) {
		Submenu submenu = new Submenu();
		submenu.setId(fEO.getFunctionName());
		submenu.setLabel(fEO.getFunctionDes());
		return submenu;
	}

	public MenuItem setMenuItem(FunctionEO fEO) {
		MenuItem menuItem = new MenuItem();
		menuItem.setId(fEO.getFunctionName());
		menuItem.setValue(fEO.getFunctionDes());
		return menuItem;
	}
	public org.icefaces.ace.component.menuitem.MenuItem setMenuItemACE(FunctionEO fEO) {
		org.icefaces.ace.component.menuitem.MenuItem menuItem = new org.icefaces.ace.component.menuitem.MenuItem();
		menuItem.setId(fEO.getFunctionName());
		menuItem.setValue(fEO.getFunctionDes());
		return menuItem;
	}
	
	// public void buldMenu(){
	// try {
	// HashMap map = getMenuBar();
	//
	// } catch (Exception e) {
	// // TODO: handle exception
	// }
	// }
	// private void buld(List mnuBar, List list){
	// MenuItem mi = null;
	// mnuBar = new ArrayList();
	// Iterator it2 = list.iterator();
	// while(it2.hasNext()){
	// Object obj = it2.next();
	// FunctionEO f = (FunctionEO)obj;
	// if(f.getParentId() == 0){
	// mi = setMenuItem(f);
	// mnuBar.add(mi);
	// list.remove(obj);
	// }
	// }
	//
	// Iterator it = mnuBar.iterator();
	//
	// Vector v = null;
	// int i = 0;
	// while(it.hasNext()){
	// Object obj = it.next();
	// MenuItem item = (MenuItem)obj;
	// it2 = list.iterator();
	//
	// while(it2.hasNext()){
	// obj = it2.next();
	// FunctionEO f = (FunctionEO)obj;
	// if(f.getParentId() == 0){
	// mi = setMenuItem(f);
	// item.getChildren().add(mi);
	// list.remove(obj);
	// mnuBar.set(i, item);
	// }
	// }
	// i++;
	// }
	// }
}
